package com.aimosheng.common.dialog;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

import androidx.appcompat.widget.DrawableUtils;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.aimosheng.common.Constants;
import com.aimosheng.common.R;
import com.aimosheng.common.adapter.CommonShareAdapter;
import com.aimosheng.common.glide.ImgLoader;
import com.aimosheng.common.interfaces.OnItemClickListener;
import com.aimosheng.common.mob.MobBean;
import com.aimosheng.common.mob.MobCallback;
import com.aimosheng.common.mob.MobShareUtil;
import com.aimosheng.common.utils.BitmapUtil;
import com.aimosheng.common.utils.ToastUtil;
import com.scwang.smartrefresh.layout.util.DensityUtil;
import com.youth.banner.loader.ImageLoader;

import java.io.File;

import cn.qqtheme.framework.util.ScreenUtils;

/**
 * Created by cxf on 2018/10/19.
 * 直播分享弹窗
 */

public class PosterShareDialogFragment extends AbsDialogFragment implements OnItemClickListener<MobBean>, View.OnClickListener {

    private ActionListener mActionListener;
    private String url;
    private ImageView mIvPoster;
    private ProgressBar mPb;
    private boolean isDownloaded;
    private MobShareUtil mMobShareUtil;
    private Bitmap mBitmap;

    public PosterShareDialogFragment(String url) {
        this.url = url;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_poster_share;
    }

    @Override
    protected int getDialogStyle() {
        return R.style.dialog;
    }

    @Override
    protected boolean canCancel() {
        return true;
    }

    @Override
    protected void setWindowAttributes(Window window) {
        window.setWindowAnimations(R.style.bottomToTopAnim);
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = ScreenUtils.widthPixels(getContext()) - DensityUtil.dp2px(60);
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        params.gravity = Gravity.CENTER;
        window.setAttributes(params);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mIvPoster = mRootView.findViewById(R.id.iv_poster);
        mPb = mRootView.findViewById(R.id.progressBar);
        mRootView.findViewById(R.id.iv_close).setOnClickListener(this);
        mRootView.findViewById(R.id.btn_wx).setOnClickListener(this);
        mRootView.findViewById(R.id.btn_wx_circle).setOnClickListener(this);
        mRootView.findViewById(R.id.btn_download).setOnClickListener(this);
        mPb.setVisibility(View.VISIBLE);
        ImgLoader.displayDrawable(getContext(), url, new ImgLoader.DrawableCallback() {
            @Override
            public void onLoadSuccess(Drawable drawable) {
                mPb.setVisibility(View.GONE);
                mIvPoster.setImageDrawable(drawable);
                isDownloaded = true;
                mBitmap = BitmapUtil.getInstance().DrawableToBitmap(drawable);
            }

            @Override
            public void onLoadFailed() {
                mPb.setVisibility(View.GONE);
            }
        });
    }

    @Override
    public void onItemClick(MobBean bean, int position) {
        if (!canClick()) {
            return;
        }
        dismiss();
        if (mActionListener != null) {
            mActionListener.onItemClick(bean.getType());
        }
    }

    @Override
    public void onClick(View v) {
        if (!canClick()) {
            return;
        }
        if (v.getId() == R.id.btn_wx) {
            share(Constants.MOB_WX);
        } else if (v.getId() == R.id.btn_wx_circle) {
            share(Constants.MOB_WX_PYQ);
        } else if (v.getId() == R.id.btn_download) {
            download();
        } else if (v.getId() == R.id.iv_close) {
            dismiss();
        }
    }

    private void download() {
        if (mBitmap == null || mBitmap.isRecycled())
            return;
        String path = BitmapUtil.getInstance().saveBitmap(getContext(), mBitmap);
        if (!TextUtils.isEmpty(path)) {
            //刷新相册
            ToastUtil.show("已保存到相册");
        }
    }


    private void share(String type) {
        if (mBitmap == null || mBitmap.isRecycled())
            return;
        if (mMobShareUtil == null) {
            mMobShareUtil = new MobShareUtil();
        }
        mMobShareUtil.executeImage(type, url, new MobCallback() {
            @Override
            public void onSuccess(Object data) {

            }

            @Override
            public void onError() {

            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onFinish() {

            }
        });
        dismiss();
    }

    public interface ActionListener {
        void onItemClick(String type);
    }

    public void setActionListener(ActionListener actionListener) {
        mActionListener = actionListener;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mContext = null;
        mActionListener = null;
        mBitmap.recycle();
        mBitmap = null;
    }
}
